from mcp.server.fastmcp import FastMCP, Context
import subprocess
import json
from typing import List, Optional

mcp = FastMCP("Intel CMT CAT MCP Server")

"""
面向软件：intel-cmt-cat

所有 MCP tool 列表：
1. pqos_help - 显示 pqos 命令的帮助信息
   参数：无
   返回：{"success": bool, "command": str, "exit_code": int, "stdout": str, "stderr": str}

2. pqos_status - 显示 L3CA COS 定义等状态信息
   参数：无
   返回：{"success": bool, "command": str, "exit_code": int, "stdout": str, "stderr": str}

3. pqos_hardware_capabilities - 显示硬件能力信息
   参数：无
   返回：{"success": bool, "command": str, "exit_code": int, "stdout": str, "stderr": str}

4. pqos_apply_config - 应用配置文件进行分配重置
   参数：config_file (str) - 配置文件的路径
   返回：{"success": bool, "command": str, "exit_code": int, "stdout": str, "stderr": str}

5. pqos_version - 显示 PQoS 库版本信息
   参数：无
   返回：{"success": bool, "command": str, "exit_code": int, "stdout": str, "stderr": str}
"""

@mcp.tool()
def pqos_help(ctx: Context) -> dict:
    """显示 pqos 命令的帮助信息
    
    返回:
        dict: 包含命令执行结果的统一JSON结构
    """
    command = ["pqos", "-h"]
    result = subprocess.run(command, capture_output=True, text=True)
    
    return {
        "success": result.returncode == 0,
        "command": " ".join(command),
        "exit_code": result.returncode,
        "stdout": result.stdout,
        "stderr": result.stderr
    }

@mcp.tool()
def pqos_status(ctx: Context) -> dict:
    """显示 L3CA COS 定义等状态信息
    
    返回:
        dict: 包含命令执行结果的统一JSON结构
    """
    command = ["pqos", "-s"]
    result = subprocess.run(command, capture_output=True, text=True)
    
    return {
        "success": result.returncode == 0,
        "command": " ".join(command),
        "exit_code": result.returncode,
        "stdout": result.stdout,
        "stderr": result.stderr
    }

@mcp.tool()
def pqos_hardware_capabilities(ctx: Context) -> dict:
    """显示硬件能力信息
    
    返回:
        dict: 包含命令执行结果的统一JSON结构
    """
    command = ["pqos", "-d"]
    result = subprocess.run(command, capture_output=True, text=True)
    
    return {
        "success": result.returncode == 0,
        "command": " ".join(command),
        "exit_code": result.returncode,
        "stdout": result.stdout,
        "stderr": result.stderr
    }

@mcp.tool()
def pqos_apply_config(ctx: Context, config_file: str) -> dict:
    """应用配置文件进行分配重置
    
    参数:
        config_file (str): 配置文件的路径
        
    返回:
        dict: 包含命令执行结果的统一JSON结构
    """
    command = ["pqos", "-f", config_file]
    result = subprocess.run(command, capture_output=True, text=True)
    
    return {
        "success": result.returncode == 0,
        "command": " ".join(command),
        "exit_code": result.returncode,
        "stdout": result.stdout,
        "stderr": result.stderr
    }

@mcp.tool()
def pqos_version(ctx: Context) -> dict:
    """显示 PQoS 库版本信息
    
    返回:
        dict: 包含命令执行结果的统一JSON结构
    """
    command = ["pqos", "--version"]
    result = subprocess.run(command, capture_output=True, text=True)
    
    return {
        "success": result.returncode == 0,
        "command": " ".join(command),
        "exit_code": result.returncode,
        "stdout": result.stdout,
        "stderr": result.stderr
    }

if __name__ == "__main__":
    mcp.run()